package uk.ac.lkl.takteen.energyapp.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class EnergyAppDatabase extends SQLiteOpenHelper {

	private static final String DEBUG_TAG = "EnergyAppDatabase";
	private static final String DB_NAME = "EnergyAppData";
	private static final int DB_VERSION = 1;

	public EnergyAppDatabase(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// create user challenges table
		db.execSQL("create table "
				+ EnergyAppDatabaseMetadata.USER_CHALLENGES_TABLE_NAME + " ("
				+ EnergyAppDatabaseMetadata._ID
				+ " integer primary key autoincrement, "
				+ EnergyAppDatabaseMetadata.CHALLENGE_NAME + " TEXT, "
				+ EnergyAppDatabaseMetadata.CREATED_BY + " TEXT, "
				+ EnergyAppDatabaseMetadata.LAST_MODIFIED_BY + " TEXT, "
				+ EnergyAppDatabaseMetadata.CHALLENGE_TYPE + " TEXT, "
				+ EnergyAppDatabaseMetadata.CHALLENGE_TYPE_POS + " INTEGER, "
				+ EnergyAppDatabaseMetadata.ENERGY_TYPE + " TEXT, "
				+ EnergyAppDatabaseMetadata.ENERGY_TYPE_POS + " INTEGER, "
				+ EnergyAppDatabaseMetadata.CHALLENGE_IMAGE + " TEXT, "
				+ EnergyAppDatabaseMetadata.CHALLENGE_INFO + " TEXT, "
				+ EnergyAppDatabaseMetadata.CHALLENGE_LINK + " TEXT);");
		// create received challenges table
		db.execSQL("create table "
				+ EnergyAppDatabaseMetadata.RECEIVED_CHALLENGES_TABLE_NAME
				+ " (" + EnergyAppDatabaseMetadata._ID
				+ " integer primary key autoincrement, "
				+ EnergyAppDatabaseMetadata.CHALLENGE_NAME + " TEXT, "
				// + EnergyAppDatabaseMetadata.CREATED_BY + " TEXT, "
				// + EnergyAppDatabaseMetadata.LAST_MODIFIED_BY + " TEXT, "
				// + EnergyAppDatabaseMetadata.CHALLENGE_TYPE + " TEXT, "
				// + EnergyAppDatabaseMetadata.CHALLENGE_TYPE_POS + " INTEGER, "
				// + EnergyAppDatabaseMetadata.ENERGY_TYPE + " TEXT, "
				// + EnergyAppDatabaseMetadata.ENERGY_TYPE_POS + " INTEGER, "
				// + EnergyAppDatabaseMetadata.MOTIVATIONAL_MSG + " TEXT, "
				+ EnergyAppDatabaseMetadata.CHALLENGE_IMAGE + " TEXT);");
		// + EnergyAppDatabaseMetadata.CHALLENGE_INFO + " TEXT, "
		// + EnergyAppDatabaseMetadata.CHALLENGE_LINK + " TEXT);");
		// create user pledges table
		db.execSQL("create table "
				+ EnergyAppDatabaseMetadata.USER_PLEDGES_TABLE_NAME + " ("
				+ EnergyAppDatabaseMetadata._ID
				+ " integer primary key autoincrement, "
				+ EnergyAppDatabaseMetadata.PLEDGE_NAME + " TEXT, "
				+ EnergyAppDatabaseMetadata.CREATED_BY + " TEXT, "
				+ EnergyAppDatabaseMetadata.DATE_CREATED + " TEXT, "
				+ EnergyAppDatabaseMetadata.WHEN_STATEMENT + " TEXT, "
				+ EnergyAppDatabaseMetadata.THEN_STATEMENT + " TEXT, "
				+ EnergyAppDatabaseMetadata.EXPIRATION_YEAR + " INTEGER, "
				+ EnergyAppDatabaseMetadata.EXPIRATION_MONTH + " INTEGER, "
				+ EnergyAppDatabaseMetadata.EXPIRATION_DAY + " INTEGER, "
				+ EnergyAppDatabaseMetadata.KUDOS + " INTEGER" + ");");
		db.execSQL("create table "
				+ EnergyAppDatabaseMetadata.SHARED_PLEDGES_TABLE_NAME + " ("
				+ EnergyAppDatabaseMetadata._ID
				+ " integer primary key autoincrement, "
				+ EnergyAppDatabaseMetadata.PLEDGE_NAME + " TEXT, "
				+ EnergyAppDatabaseMetadata.PLEDGE_IMAGE + " TEXT);");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(DEBUG_TAG,
				"Upgrading database. Existing contents will be lost. ["
						+ oldVersion + "]->[" + newVersion + "]");
		db.execSQL("DROP TABLE IF EXISTS "
				+ EnergyAppDatabaseMetadata.USER_CHALLENGES_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS "
				+ EnergyAppDatabaseMetadata.RECEIVED_CHALLENGES_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS "
				+ EnergyAppDatabaseMetadata.USER_PLEDGES_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS "
				+ EnergyAppDatabaseMetadata.SHARED_PLEDGES_TABLE_NAME);
		onCreate(db);
	}

}
